// pages/recruitment_homepage/recruitment_homepage.js
const app = getApp(), core = require('../../utils/core'), util = require('../../utils/util');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    group_id:1,
    isFav:false,
    nav:0,
    jobs: [],
    comments: [],
  },
  bindToggleFav:function(){
    if(!app.getUserinfo(true)) {
      return;
    }
    let page = this;
    page.setData({isFav: !this.data.isFav});
    core.post({url: '/xilujob.company/toggle_fav',loading:false,data:{company_id: this.data.companyId},success:(ret) => {
      page.setData({isFav: ret.data.is_fav});
      if(this.data.isFav) {
        app.eventBus.emit(app.Events.CompanyFavAdd, {company: this.data.company});
      }else {
        app.eventBus.emit(app.Events.CompanyFavRemove, {company: this.data.company});
      }
    },fail:ret=>{
      wx.showToast({title: '收藏失败',icon:'none'});
      page.setData({isFav: !page.data.isFav});
      return false;
    }});
  },
  status:function(e){
    this.setData({
      nav:e.currentTarget.dataset.index
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let group_id = app.getUserinfo() ? app.getUserinfo().group_id : 1;
    this.setData({companyId: options.id,group_id:group_id});
    this.getCompanyDetail();
    core.get({url:'xilujob.company/jobs',data:{company_id:this.data.companyId},success:(ret)=>{this.setData({jobs:ret.data.jobs})}});
    //core.get({url:'xilujob.company_comment/lists', data:{id:this.data.companyId,pagesize:5}, success:ret=>this.setData({comments:ret.data.company_comments})});
    app.eventBus.on(app.Events.UserLogin, this.eventOnUserLogin, this);
  },
  //溝通
  contact(){
    if(!app.getUserinfo(true)) {
      return;
    }
    wx.navigateTo({url: '/pages/communicate/index?puser_id='+this.data.company.user_id});
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    app.eventBus.on(app.Events.UserLogin, this.eventOnUserLogin);
  },

  getCompanyDetail() {
    core.get({
      url: '/xilujob.company/detail', data: {company_id:this.data.companyId},
      success: ret => {
        let company = ret.data.company;
        let images = company.enterpriseinfo.environment_images;
        images = images ? images.split(',') : [];
        let markers = [];
        if(company.enterpriseinfo.lat!=0) {
          markers.push({
            id: 0,
            latitude: company.enterpriseinfo.lat,
            longitude: company.enterpriseinfo.lng,
            width: 35,
            height: 35,
            callout: {
              content: company.enterpriseinfo.address, //文本
              color: '#000000', //文本颜色
              borderRadius: 3, //边框圆角
              borderWidth: 1, //边框宽度
              borderColor: '#ffffff', //边框颜色
              bgColor: '#ffffff', //背景色
              padding: 5, //文本边缘留白
              display: "ALWAYS",
              textAlign: 'center' //文本对齐方式。有效值: left, right, center
            }
          });
        }
        this.setData({
          company: company, isFav: ret.data.is_fav, commentCount: ret.data.comment_count,
          images: images, markers: markers, showCommentTip: ret.data.can_comment
        });
      },fail: ret => {
        wx.navigateBack();
      }
    });
  },

  bindShowImages() {
    wx.navigateTo({url: '/pages/business_image/business_image',success:(res)=>{
      res.eventChannel.emit('dataTransfer',{images:this.data.images});
    }});
  },
  bindShowLocation() {
    wx.openLocation({
      latitude: this.data.company.enterpriseinfo.lat,
      longitude: this.data.company.enterpriseinfo.lng,
      name: this.data.company.enterpriseinfo.full_name||this.data.company.enterpriseinfo.company_name,
      address: this.data.company.enterpriseinfo.address,
    });
  },
  hideCommentTip() {
    this.setData({showCommentTip: false});
  },
  createCompanyComment() {
    wx.navigateTo({url: '/pages/post_company_comment/post_company_comment?id='+this.data.company.id, success:(res)=>{
      res.eventChannel.emit('dataTransfer',{company:this.data.company});
    }});
    this.hideCommentTip();
  },

  eventOnUserLogin(userinfo) {
    this.getCompanyDetail();
  },
  onShareAppMessage() {
    let title = '西陆招聘 - ' + this.data.company.enterpriseinfo.full_name||this.data.company.enterpriseinfo.company_name;
    let id = this.data.company.id;
    return {
      title: title,
      path: '/pages/recruitment_homepage/recruitment_homepage?id='+id,
    };
  },
  share() {
    core.get({url:'/xilujob.company/poster_info/id/'+this.data.company.id, success:ret=>{
      core.setCache('temp',ret.data,10);
      wx.navigateTo({
        url: '/pages/share_post_new/share_post_new'
      })
    }});
  },
  shareTip() {
    this.setData({showShareTip: !this.data.showShareTip});
  }

});
